package com.drcoffee.drcoffeemall.controller;

import com.drcoffee.drcoffeemall.entry.*;
import com.drcoffee.drcoffeemall.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.security.GeneralSecurityException;
import java.util.HashMap;
import java.util.Map;

/**
 * 用户
 *
 * @author ShiHao Zhou
 */
@RequestMapping("/user")
@RestController
public class UserController {

    @Autowired
    UserService userService;

    @Autowired
    RedisTemplate<String, Object> redisTemplate;

    /*
     * 发送手机验证码
     * */
    @GetMapping("/sendPhoneCode")
    public Result sendPhoneCode(@RequestParam String phone) {
        return userService.sendPhoneCode(phone);
    }

    @GetMapping("/verifyCode")
    public Result verifyCode(@RequestParam String v1, @RequestParam String v2, @RequestParam String v3) {
        // v1:key ,v2:val ,v3:校验之后是否删除
        return userService.verifyCode(v1, v2, v3);
    }

    /*
     * 校验账号是否已被注册
     * */
    @GetMapping("/verifyAccount")
    public Result verifyAccount(@RequestParam String account) {
        return userService.verifyAccount(account);
    }

    /*
     * 注册账号
     * */
    @PostMapping("/register")
    public Result register(@RequestBody User user) throws GeneralSecurityException {
        return userService.register(user);
    }

    @PostMapping("/setAccount")
    public Result setAccount(@RequestBody User user) {
        return userService.setAccount(user);
    }

    /*
     * 找回密码
     * */
    @PostMapping("/retrievePass")
    public Result retrievePass(@RequestBody Map<String, Object> reval) throws GeneralSecurityException {
        return userService.retrievePass(reval);
    }

    /*
     * 登录验证
     * */
    @PostMapping("/accountLogin")
    public Result accountLogin(@RequestBody User user){
        return userService.accountLogin(user);
    }

    /*
     * 验证码登录
     * */
    @GetMapping("/codeLogin")
    public ResponseEntity<Object> codeLogin(@RequestParam String key, @RequestParam String code, @RequestParam Integer type) {
        return userService.codeLogin(key, code, type);
    }

    /*
     * 判断是否为管理员
     * */
    @PostMapping("/allPermissionCheck")
    public Map<String, Object> allPermissionCheck(@RequestHeader String Token) {
        return userService.allPermissionCheck(Token);
    }

    /*
     * 删除Token
     * */
    @PostMapping("/removeToken")
    public boolean removeToken(@RequestHeader String Token) {
        return userService.removeToken(Token);
    }

    /*
     * 更新Token
     * */
    @PostMapping("/renewalToken")
    public void renewalToken(@RequestHeader String Token) {
        userService.renewalToken(Token);
    }

    /*
     * 验证Token
     * */
    @GetMapping("/verifyToken")
    public boolean verifyToken(@RequestHeader String Token) {
        System.out.println(redisTemplate.opsForValue().get(Token));
        return redisTemplate.opsForValue().get(Token) != null;
    }

    /*
     * 登录验证码
     * */
    @GetMapping("/sendLoginCode")
    public Result sendLoginCode(@RequestParam String val) {
        return userService.sendLoginCode(val);
    }

    /*
     * 校验邮箱验证码
     * */
    @GetMapping("/verifyEmailCode")
    public boolean verifyEmailCode(@RequestParam String email, @RequestParam String email_code) {
        return userService.verifyEmailCode(email, email_code);
    }

    /*
     * 发送邮箱验证码(找回密码)
     * */
    @GetMapping("/sendEmailCode")
    public boolean sendEmailCode(@RequestParam String email, Integer type) {
        return userService.sendEmailCode(email, type);
    }

    /*
     * 设置为默认地址
     * */
    @GetMapping("/setDefaultAddress")
    public Result setDefaultAddress(@RequestParam Integer id, @RequestParam String account) {
        return userService.setDefaultAddress(id, account);
    }

    /*
     * 修改用户信息
     * */
    @PostMapping("/uploadUser")
    public Map<String, Object> uploadUser(@RequestBody User user) {
        return userService.uploadUser(user);
    }

    /*
     * 修改用户头像
     * */
    @GetMapping("/uploadUserPhoto")
    public Result uploadUserPhoto(@RequestParam Integer id, @RequestParam String val1, @RequestParam String val2) {
        return userService.uploadUserPhoto(id, val1, val2);
    }

    /*
     * 根据账号查询用户
     * */
    @GetMapping("/getUserByAccount")
    public Result getUserByAccount(@RequestParam String account) {
        return userService.getUserByAccount(account);
    }

    /*
     * 判断邮箱或手机号是否存在
     * */
    @GetMapping("/isExist")
    public ResponseEntity<Object> isExist(@RequestParam String val) {
        return userService.isExist(val);
    }

    /*
     * 更改密码
     * */
    @PostMapping("/changePass")
    public Result changePass(@RequestBody Map<String, Object> val) {
        return userService.changePass(val);
    }

    /*
     * 更改邮箱
     * */
    @GetMapping("/changeEmPh")
    public Result changeEmPh(@RequestHeader String Token, @RequestParam String v1, @RequestParam String v2, @RequestParam String v3) {
        return userService.changeEmPh(Token, v1, v2, v3);
    }

    /*
     * 更改名称
     * */
    @GetMapping("/changeName")
    public Result changeName(@RequestHeader String Token, @RequestParam String newName) {
        return userService.changeName(Token, newName);
    }

    /*
     * 邀请码校验
     * */
    @GetMapping("/invitationCheck")
    public Result invitationCheck(@RequestParam Integer code) {
        return userService.invitationCheck(code);
    }

    /*
     * 添加反馈
     * */
    @PostMapping("/putFeedback")
    public Result putFeedback(@RequestHeader String Token, @RequestBody Feedback feedback) {
        return userService.putFeedback(Token, feedback);
    }

    /*
     * 判断是否存在未审核的企业变更记录
     * */
    @GetMapping("/getAlterFirmPD")
    public Result getAlterFirmPD(@RequestParam String account) {
        return userService.getAlterFirmPD(account);
    }

    /*
     * 添加企业变更申请
     * */
    @PostMapping("/addFirmAlter")
    public Result addFirmAlter(@RequestBody FirmAlter firmAlter) {
        return userService.addFirmAlter(firmAlter);
    }

    /*
     * 判断账号是否注册
     * 已经注册返回 1
     * */
    @GetMapping("/haveRegistered")
    public ResponseEntity<Integer> haveRegistered(@RequestParam String email) {
        return userService.haveRegistered(email);
    }

}
